/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
 
 **** LICENSE: ****
 
        This library is free software; you can redistribute it and/or
        modify it under the terms of the GNU Lesser General Public
        License as published by the Free Software Foundation,
        version 2.1.
 
        This library is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        Lesser General Public License for more details.
 
        You should have received a copy of the GNU Lesser General Public
        License along with this library; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 **** CONTACT: ****
 
    I can be reached through the jmonkeyengine.com forums (user "llama"),
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5
 
 */

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.pass;

import java.io.IOException;
import java.util.HashSet;

import org.llama.jmex.task.TaskRunner;
import org.llama.jmex.terra.HeightMapManager;
import org.llama.jmex.terra.TerraManager;
import org.llama.jmex.terra.TerraView;
import org.llama.jmex.terra.TerraViewTask;
import org.llama.jmex.terra.XYKey;

public class DistanceLoadingPass extends DistancePass {
    
    private boolean compressed;
    private TaskRunner runner;
    private TaskRunner textureRunner;
    
    public DistanceLoadingPass(int distance, String key, TaskRunner runner, TaskRunner textureRunner) {
        super(distance, key, false);
        compressed = HeightMapManager.K_COMPRESSED.equals(key);
        this.runner = runner;
        this.textureRunner = runner;
    }
    
    public void remove(XYKey xy, TerraView tv) {
//System.out.println("loadinPass.remove: "+xy);
        final TerraManager tm = tv.getTerraManager();
//System.out.println("loadinPass.removed: "+xy);

    }
    
    private HashSet unloadable = new HashSet();
    
    public void add(XYKey xy, final TerraView tv) {
//System.out.println("loadinPass.add: "+xy);
        final TerraManager tm = tv.getTerraManager();
        
        Object o = compressed ? (Object)tm.getCompressedMap(xy.x, xy.y) : (Object)tm.getMap(xy.x, xy.y);
        if (o != null)
            return;
        
        if (compressed)
            xy.update(xy.x, xy.y, HeightMapManager.K_COMPRESSED);
        else
            xy.update(xy.x, xy.y, HeightMapManager.K_MAP);
        
        if (tv.getTaskManager().containsTask(xy) || unloadable.contains(xy))
            return;
        
        xy = new XYKey(xy);
        
        TerraViewTask task = new TerraViewTask(tv, xy) {
            
            public boolean runTask() {
                try {
//					System.out.print("-----> Load Compressed: " + xy);
                    long count = System.currentTimeMillis();
                    if (! (
                            (compressed && tm.loadCompressedMap(xy.x, xy.y) )
                            
                            ||
                            
                            (!compressed && tm.loadMap(xy.x, xy.y))
                            
                            )) {
                        throw new IOException("error loading!");
//						Thread.sleep(1000);
                    } else {
//                        System.out.println(" Loading took " + (System.currentTimeMillis() - count)
//                        + " " + xy);
                    }
                    tv.add(xy);
                    
                } catch (IOException e) {
                    e.printStackTrace();
                    unloadable.add(xy);
                }
                tv.updateNextFrame();
                return true;
            }
            
            public String toString() {
                return new String("Task: " + xy + " " + priority);
            }
            
        };
        
        tv.getTaskManager().addTask(task, runner);
//System.out.println("loadinPass.added: "+xy);
    }
    
    
    
}
